import { Dictionary } from "../../utils/dictionary";
import { helpEntity } from "./helpEntity";

/**
 * @Author: 陈瑞鹏
 * @Date:   2023-01-30 09:40:25
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-01-31 16:18:44
 */
export default class helpCtr {

  private static _ins: helpCtr = null;

  public static get ins(): helpCtr {
    if (this._ins == null) {
      this._ins = new helpCtr();
    }
    return this._ins;
  }

  public helpDic: Dictionary<any, helpEntity>;

  public totalWeight: number = 0;

  public curHelpEntity: helpEntity = null;

  public initTp(list) {
    this.helpDic = new Dictionary();
    list.forEach(element => {
      if (element) {
        let helpEny: helpEntity = new helpEntity();
        helpEny.init(element);
        this.helpDic.set(helpEny.id, helpEny);
      }
    });

    let helpList = this.helpDic.getValueList();

    this.totalWeight = helpList.reduce(function (pre, cur, index) {
      cur.startW = pre;
      return cur.endW = pre + cur.pro
    }, 0);
  }

  /**
   * 获取三个助力
   * @returns 
   */
  public getThreeEntity() {
    let arr = [];
    let list = this.helpDic.getValueList();
    let that = this;
    let rand = function (p) {
      let random = Math.ceil(Math.random() * that.totalWeight)
      let selectPeople = p.find(people => people.startW < random && people.endW > random)
      return selectPeople;
    };
    while (arr.length < 3) {
      let p = rand(list);
      let bool = false;
      arr.forEach(ele => {
        if (ele && p && ele.id == p.id) {
          bool = true;
        }
      })
      if (!bool) {
        arr.push(p);
      }
    }
    return arr;
  }

  /**
 * 雇佣士兵花费金币减少比例
 */
  public get goldXs() {
    let helpXs = 0;
    if (helpCtr.ins.curHelpEntity && helpCtr.ins.curHelpEntity.id == 1) {
      helpXs = 0.1;
    }
    return 1 - helpXs;
  }

  /**
   * 火焰燃烧时间 增加
   */
  public get fireAdd() {
    let helpAdd = 0;
    if (helpCtr.ins.curHelpEntity && helpCtr.ins.curHelpEntity.id == 2) {
      helpAdd = 3;
    }
    return helpAdd;
  }

  /**
   * 金矿刷新间隔 减少
   */
  public get refresh() {
    let helpXs = 0;
    if (helpCtr.ins.curHelpEntity && helpCtr.ins.curHelpEntity.id == 3) {
      helpXs = 0.1;
    }
    return 1 - helpXs;
  }

  /**
   * 额外增加金币
   */
  public get goldAdd() {
    let helpAdd = 0;
    if (helpCtr.ins.curHelpEntity && helpCtr.ins.curHelpEntity.id == 4) {
      helpAdd = 300;
    } else if (helpCtr.ins.curHelpEntity && helpCtr.ins.curHelpEntity.id == 6) {
      helpAdd = 350;
    }
    return helpAdd;
  }

  /**
   * 获得两门大炮
   */
  public get cannon() {
    let helpAdd = 0;
    if (helpCtr.ins.curHelpEntity && helpCtr.ins.curHelpEntity.id == 5) {
      helpAdd = 2;
    }
    return helpAdd;
  }

  /**
   * 四周的火焰会燃烧敌人
   */
  public get isAroundFire() {
    let bool = false;
    if (helpCtr.ins.curHelpEntity && helpCtr.ins.curHelpEntity.id == 6) {
      bool = true;
    }
    return bool;
  }

  /**
   * 树木与地雷额外加1金币产出
   */
  public get extraGold() {
    let helpAdd = 0;
    if (helpCtr.ins.curHelpEntity && helpCtr.ins.curHelpEntity.id == 7) {
      helpAdd = 1;
    }
    return helpAdd;
  }

  /**
   * 训练犬的采集速度增加50%
   */
  public get harvestSpeed() {
    let helpXs = 0;
    if (helpCtr.ins.curHelpEntity && helpCtr.ins.curHelpEntity.id == 8) {
      helpXs = 0.5;
    }
    return 1 + helpXs;
  }

  /**
   * 给城堡增加一个魔法球环绕物
   */
  public get isBall() {
    let bool = false;
    if (helpCtr.ins.curHelpEntity && helpCtr.ins.curHelpEntity.id == 9) {
      bool = true;
    }
    return bool;
  }

  /**
  * 城堡加20点生命值
  */
  public get extraHp() {
    let helpAdd = 0;
    if (helpCtr.ins.curHelpEntity && helpCtr.ins.curHelpEntity.id == 10) {
      helpAdd = 20;
    }
    return helpAdd;
  }
}